﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace CloudPms.Common.Utils
{
    /// <summary>
    /// 通用工具
    /// </summary>
    public class ComTools
    {
        /// <summary>
        /// 判断是否为数字
        /// </summary>
        /// <param name="snum"></param>
        /// <returns></returns>
        public static bool IsNumberic(string snum)
        {
            int temnum;
            bool res = Int32.TryParse(snum, out temnum);
            return res;
        }
        /// <summary>
        /// 判断是否为decimal类型
        /// </summary>
        /// <param name="snum"></param>
        /// <returns></returns>
        public static bool IsDecimal(string snum)
        {
            decimal temnum;
            bool res = Decimal.TryParse(snum, out temnum);
            return res;
        }
        /// <summary>
        /// 根据班次中文名，获取对应的ＩＤ
        /// 1 早早班
        /// 2 早班
        /// 3 中班
        /// 4 晚班 
        /// </summary>
        /// <param name="workShiftId"></param>
        /// <returns></returns>
        public static  string workShiftTran(string workShiftId)
        {
            switch (workShiftId)
            {
                case "早早班":
                    return "1";
                case "早班":
                    return "2";
                case "中班":
                    return "3";
                case "晚班":
                    return "4";
                default :
                    return "";
            }
        }

        /// <summary>
        /// 拼接备注字符
        /// </summary>
        /// <param name="oldNotetxt"></param>
        /// <param name="newNotetxt"></param>
        /// <returns></returns>
        public static  string NoteBuild(string oldNotetxt,string newNotetxt)
        {
            newNotetxt = newNotetxt.Replace('/', '$');
            if ((oldNotetxt??string.Empty).Trim()==string.Empty)
            {
                oldNotetxt = newNotetxt;
            }
            else
            {
                oldNotetxt = oldNotetxt + "/" + newNotetxt;
            }
            if(oldNotetxt.Length>500)
            {
                oldNotetxt = oldNotetxt.Substring(oldNotetxt.Length-500);
            }
            return oldNotetxt;
        }
        /// <summary>
        /// 拼接备注字符
        /// </summary>
        /// <param name="oldNotetxt"></param>
        /// <param name="newNotetxt"></param>
        /// <returns></returns>
        public static string NoteBuild(string oldNotetxt, string newNotetxt,int maxlength)
        {
            newNotetxt = newNotetxt.Replace('/', '$');
            if ((oldNotetxt??string.Empty).Trim()==string.Empty)
            {
                oldNotetxt = newNotetxt;
            }
            else
            {
                oldNotetxt = oldNotetxt + "/" + newNotetxt;
            }
            
            if (oldNotetxt.Length > maxlength)
            {
                oldNotetxt = oldNotetxt.Substring(oldNotetxt.Length - maxlength);
            }
            return oldNotetxt;
        }
        /// <summary>
        /// 获得32位的MD5加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string GetMD5_32(string input)
        {
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] data = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(input));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sb.Append(data[i].ToString("x2"));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 获得16位的MD5加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string GetMD5_16(string input)
        {
            return GetMD5_32(input).Substring(8, 16);
        }

        /// <summary>
        /// 获得8位的MD5加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string GetMD5_8(string input)
        {
            return GetMD5_32(input).Substring(8, 8);
        }

        /// <summary>
        /// 获得4位的MD5加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string GetMD5_4(string input)
        {
            return GetMD5_32(input).Substring(8, 4);
        }

        /// <summary>
        /// 添加MD5的前缀，便于检查有无篡改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string AddMD5Profix(string input)
        {
            return GetMD5_4(input) + input;
        }

        /// <summary>
        /// 移除MD5的前缀
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string RemoveMD5Profix(string input)
        {
            return input.Substring(4);
        }

        /// <summary>
        /// 验证MD5前缀处理的字符串有无被篡改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool ValidateValue(string input)
        {
            bool res = false;
            if (input.Length >= 4)
            {
                string tmp = input.Substring(4);
                if (input.Substring(0, 4) == GetMD5_4(tmp))
                {
                    res = true;
                }
            }
            return res;
        }

        private static string KEY_64 = "Vienna88";
        private static string IV_64 = "ShanPao_";
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Encode(string data)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            int i = cryptoProvider.KeySize;
            MemoryStream ms = new MemoryStream();
            CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

            StreamWriter sw = new StreamWriter(cst);
            sw.Write(data);
            sw.Flush();
            cst.FlushFinalBlock();
            sw.Flush();
            return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);

        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Decode(string data)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

            byte[] byEnc;
            try
            {
                byEnc = Convert.FromBase64String(data);
            }
            catch
            {
                return null;
            }
            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream ms = new MemoryStream(byEnc);
            CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
            StreamReader sr = new StreamReader(cst);
            return sr.ReadToEnd();
        }

    }
}
